package cn.test.users.web;


import cn.test.users.entity.User;
import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @auther hanchao
 * @date 2017/1/22 17:41
 */
@RestController
@RequestMapping("/users")
public class UserController {
    private final Logger logger = LoggerFactory.getLogger(getClass());

    private List<User> users = Lists.newArrayList(new User().setAge("1").setName("a"),new User().setAge("2").setName("b"));


    @Autowired
    private DiscoveryClient client;

    @RequestMapping("/test")
    public void test(){
        ServiceInstance instance = client.getLocalServiceInstance();
        logger.info("test, host:" + instance.getHost() + ", service_id:" + instance.getServiceId());
    }

    @RequestMapping("/add")
    public Integer add(int a,int b){
        ServiceInstance instance = client.getLocalServiceInstance();
        logger.info("test, host:" + instance.getHost() + ", service_id:" + instance.getServiceId());

        return a+b;
    }

    @RequestMapping(method = RequestMethod.GET)
    public List<User> findAll(){
        return users;
    }
}
